<?php
require_once 'config.php';

// 如果用户已登录，重定向到首页
if (isLoggedIn()) {
    redirect('index.php');
}

$errors = [];

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';
    $confirm_password = $_POST['confirm_password'] ?? '';
    $email = trim($_POST['email'] ?? '');
    $full_name = trim($_POST['full_name'] ?? '');
    
    // 验证用户名
    if (empty($username)) {
        $errors[] = "用户名不能为空";
    } elseif (strlen($username) < 3 || strlen($username) > 50) {
        $errors[] = "用户名长度必须在3-50个字符之间";
    } else {
        // 检查用户名是否已存在
        $stmt = $pdo->prepare("SELECT COUNT(*) FROM users WHERE username = ?");
        $stmt->execute([$username]);
        if ($stmt->fetchColumn() > 0) {
            $errors[] = "该用户名已被使用";
        }
    }
    
    // 验证密码
    if (empty($password)) {
        $errors[] = "密码不能为空";
    } elseif (strlen($password) < 6) {
        $errors[] = "密码长度不能少于6个字符";
    } elseif ($password !== $confirm_password) {
        $errors[] = "两次输入的密码不一致";
    }
    
    // 验证邮箱
    if (empty($email)) {
        $errors[] = "邮箱不能为空";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors[] = "邮箱格式不正确";
    } else {
        // 检查邮箱是否已存在
        $stmt = $pdo->prepare("SELECT COUNT(*) FROM users WHERE email = ?");
        $stmt->execute([$email]);
        if ($stmt->fetchColumn() > 0) {
            $errors[] = "该邮箱已被注册";
        }
    }
    
    // 验证姓名
    if (empty($full_name)) {
        $errors[] = "姓名不能为空";
    } elseif (strlen($full_name) > 100) {
        $errors[] = "姓名长度不能超过100个字符";
    }
    
    // 如果没有错误，则创建用户
    if (empty($errors)) {
        try {
            // 密码加密
            $hashed_password = password_hash($password, PASSWORD_DEFAULT);
            
            // 插入用户数据
            $stmt = $pdo->prepare("INSERT INTO users (username, password, email, full_name) VALUES (?, ?, ?, ?)");
            $stmt->execute([$username, $hashed_password, $email, $full_name]);
            
            // 设置成功消息
            $_SESSION['flash_message'] = "注册成功！请登录";
            $_SESSION['flash_type'] = "success";
            
            // 重定向到登录页面
            redirect('login.php');
        } catch (PDOException $e) {
            $errors[] = "注册失败: " . $e->getMessage();
        }
    }
}

// 页面标题
$page_title = "注册 - 图书借阅系统";

// 引入头部
include 'header.php';
?>

<div class="row justify-content-center">
    <div class="col-md-6">
        <div class="card">
            <div class="card-header">
                <h4>用户注册</h4>
            </div>
            <div class="card-body">
                <?php if (!empty($errors)): ?>
                    <div class="alert alert-danger">
                        <ul class="mb-0">
                            <?php foreach ($errors as $error): ?>
                                <li><?php echo htmlspecialchars($error); ?></li>
                            <?php endforeach; ?>
                        </ul>
                    </div>
                <?php endif; ?>
                
                <form method="post" action="">
                    <div class="form-group">
                        <label for="username">用户名</label>
                        <input type="text" class="form-control" id="username" name="username" value="<?php echo htmlspecialchars($_POST['username'] ?? ''); ?>" required>
                        <small class="form-text text-muted">用户名长度必须在3-50个字符之间</small>
                    </div>
                    
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" class="form-control" id="password" name="password" required>
                        <small class="form-text text-muted">密码长度不能少于6个字符</small>
                    </div>
                    
                    <div class="form-group">
                        <label for="confirm_password">确认密码</label>
                        <input type="password" class="form-control" id="confirm_password" name="confirm_password" required>
                    </div>
                    
                    <div class="form-group">
                        <label for="email">邮箱</label>
                        <input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($_POST['email'] ?? ''); ?>" required>
                    </div>
                    
                    <div class="form-group">
                        <label for="full_name">姓名</label>
                        <input type="text" class="form-control" id="full_name" name="full_name" value="<?php echo htmlspecialchars($_POST['full_name'] ?? ''); ?>" required>
                    </div>
                    
                    <button type="submit" class="btn btn-primary">注册</button>
                    <a href="login.php" class="btn btn-link">已有账号？立即登录</a>
                </form>
            </div>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?> 